package com.yyyzzz.servlet;

import com.yyyzzz.bean.Course;
import com.yyyzzz.bean.StudentCourse;
import com.yyyzzz.service.courseService;
import com.yyyzzz.service.impl.courseServiceimpl;
import com.yyyzzz.service.impl.studentServiceimpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@WebServlet("/chooseCourseServlet")
public class chooseCourseServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String courseid = request.getParameter("courseId");
        String courseName = request.getParameter("courseName");
        String courseTeacher = request.getParameter("courseTeacher");
        String classRoom = request.getParameter("classRoom");
        Double credit = Double.parseDouble(request.getParameter("credit"));
        response.setCharacterEncoding("utf-8");
        StudentCourse studentCourse = new StudentCourse();
        studentCourse.setCourseId(courseid);
        studentCourse.setCourseName(courseName);
        studentCourse.setCourseTeacher(courseTeacher);
        studentCourse.setClassroom(classRoom);
        studentCourse.setCredit(credit);
        System.out.println(courseid);
        studentServiceimpl service = new studentServiceimpl();
        boolean addCourse = service.addCourse(studentCourse);
        if(addCourse==true){
            List<StudentCourse> studentcourseList = service.getAllCourse();
            request.setAttribute("studentcourseList",studentcourseList);
        request.getRequestDispatcher("chooseCourse.jsp").forward(request, response);

        }else{
            request.setAttribute("error1","选课失败:学号为"+courseid+"的课程已存在");
            request.getRequestDispatcher("studentMain.jsp").forward(request,response);
        }




    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String courseid = request.getParameter("courseid");
        response.setCharacterEncoding("utf-8");
        courseService service = new courseServiceimpl();
        List courseList2 = service.selectOneById(courseid);
        System.out.println(courseid);
        request.setAttribute("courseList2", courseList2);
        request.getRequestDispatcher("chooseCourse.jsp").forward(request, response);
    }
}
